﻿package mortal.game.view.common.display
{
    import com.gengine.debug.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.mui.controls.*;
    import com.mui.manager.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.game.view.common.*;

    public class BitmapNumberText extends ToolTipSprite
    {
        private var _text:String;
        private var _urlName:String;
        private var _curBmps:Array;
        private var _needUpdate:Boolean = false;
        private var _cellWidth:Number = 16;
        private var _cellHeight:int = 20;
        private var _gap:int = 2;
        private var _rollingToValue:int;
        private var _rollingTweens:Array;
        private var _loaded:Boolean = false;
        private var _align:int;
        public var _picNum:int = 13;
        private var _customCharIndex:Object;
        private var _timer:Timer;
        private var _targetValue:int;
        public static const Left:int = 0;
        public static const Mid:int = 1;
        public static const Right:int = 2;
        private static var _bmpds:Dictionary = new Dictionary();
        private static const charIndex:Object = {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, (:10, ):11, -:12, +:13, /:14};

        public function BitmapNumberText()
        {
            this._curBmps = [];
            return;
        }// end function

        public function get customCharIndex() : Object
        {
            return this._customCharIndex;
        }// end function

        public function set customCharIndex(param1:Object) : void
        {
            this._customCharIndex = param1;
            return;
        }// end function

        public function get picNum() : int
        {
            return this._picNum;
        }// end function

        public function set picNum(param1:int) : void
        {
            if (_bmpds[this._urlName] != null && param1 != ((_bmpds[this._urlName] as Array).length - 1))
            {
                ThrowError.show(this._urlName + "资源的picNum设置有冲突,请检查");
            }
            this._picNum = param1;
            return;
        }// end function

        public function get align() : int
        {
            return this._align;
        }// end function

        public function set align(param1:int) : void
        {
            this._align = param1;
            this.updateLaout();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._customCharIndex = null;
            DisplayUtil.delNotUse(this._curBmps, 0, true);
            this._curBmps = [];
            this._loaded = false;
            this.disposeRolling();
            this._align = Left;
            this.stopRun();
            if (this._urlName)
            {
                LoaderManager.instance.removeResourceEvent(this._urlName, this.onLoaded);
            }
            this._urlName = "";
            super.disposeImpl(param1);
            return;
        }// end function

        protected function disposeRolling() : void
        {
            var _loc_1:BitmapNumberTextRolling = null;
            if (this._rollingTweens != null && this._rollingTweens.length > 0)
            {
                for each (_loc_1 in this._rollingTweens)
                {
                    
                    _loc_1.dispose(true);
                }
            }
            this._rollingTweens = null;
            return;
        }// end function

        public function set value(param1:int) : void
        {
            this.text = param1.toString();
            return;
        }// end function

        public function get value() : int
        {
            return int(this.text);
        }// end function

        public function set text(param1:String) : void
        {
            this.disposeRolling();
            this._text = param1;
            this._needUpdate = true;
            this.updateText();
            this.updateLaout();
            return;
        }// end function

        public function get text() : String
        {
            return this._text;
        }// end function

        override public function get width() : Number
        {
            if (this._text == null || this._text == "")
            {
                return super.width;
            }
            var _loc_1:* = this._text.length;
            return _loc_1 * this._cellWidth + (_loc_1 - 1) * this._gap;
        }// end function

        override public function get height() : Number
        {
            return this._cellHeight;
        }// end function

        public function setSytleName(param1:String, param2:Number, param3:int, param4:int = 2, param5:int = 13) : void
        {
            this._cellWidth = param2;
            this._cellHeight = param3;
            this._gap = param4;
            if (this._urlName)
            {
                LoaderManager.instance.removeResourceEvent(this._urlName, this.onLoaded);
            }
            this._urlName = param1;
            this.picNum = param5;
            if (_bmpds[this._urlName] != null)
            {
                this._loaded = true;
                this.updateText();
                return;
            }
            if (this._urlName)
            {
                LoaderManager.instance.load(this._urlName, this.onLoaded);
            }
            return;
        }// end function

        private function onLoaded(param1:ImageInfo) : void
        {
            this._loaded = true;
            if (this._urlName != null && _bmpds[this._urlName] != null)
            {
                this.updateText();
                this.updateLaout();
                return;
            }
            this.initBmps(param1.bitmapData);
            this.updateText();
            this.updateLaout();
            if (this._rollingToValue > 0)
            {
                this.rollingToValue(this._rollingToValue);
            }
            return;
        }// end function

        public function initBmps(param1:BitmapData) : void
        {
            var _loc_4:BitmapData = null;
            var _loc_2:Array = [];
            _bmpds[this._urlName] = _loc_2;
            var _loc_3:int = 0;
            while (_loc_3 <= this._picNum)
            {
                
                _loc_4 = new BitmapData(this._cellWidth, this._cellHeight, true, 0);
                _loc_4.draw(param1, new Matrix(1, 0, 0, 1, (-_loc_3) * this._cellWidth));
                _loc_2.push(_loc_4);
                _loc_3++;
            }
            return;
        }// end function

        public function addToValue(param1:int) : void
        {
            this._targetValue = param1;
            if (this._timer == null)
            {
                this._timer = new Timer(50);
                this._timer.addEventListener(TimerEvent.TIMER, this.onRuning);
            }
            else
            {
                this._timer.delay = 5;
                this._timer.stop();
                this._timer.reset();
            }
            this._timer.start();
            return;
        }// end function

        private function onRuning(event:TimerEvent) : void
        {
            var _loc_6:int = 0;
            var _loc_2:* = (this._targetValue - int(this.text) >> 1) + 1;
            this.text = (int(this.text) + _loc_2).toString();
            var _loc_3:* = this._curBmps.length - _loc_2.toString().length;
            var _loc_4:* = FilterConst.RollFilter;
            var _loc_5:* = this._curBmps.length;
            while (_loc_6 < _loc_5)
            {
                
                if (_loc_6 >= _loc_3)
                {
                    if ((this._curBmps[_loc_6] as GBitmap).filters.length == 0)
                    {
                        (this._curBmps[_loc_6] as GBitmap).filters = [_loc_4];
                    }
                }
                else
                {
                    (this._curBmps[_loc_6] as GBitmap).filters = [];
                }
                _loc_6++;
            }
            if (int(this.text) >= this._targetValue)
            {
                this.text = this._targetValue.toString();
                this.stopRun();
            }
            return;
        }// end function

        private function stopRun() : void
        {
            var _loc_2:int = 0;
            if (this._timer)
            {
                this._timer.stop();
                this._timer.removeEventListener(TimerEvent.TIMER, this.onRuning);
                this._timer = null;
            }
            var _loc_1:* = this._curBmps.length;
            while (_loc_2 < _loc_1)
            {
                
                (this._curBmps[_loc_2] as GBitmap).filters = [];
                _loc_2++;
            }
            return;
        }// end function

        public function rollingToValue(param1:int) : void
        {
            var _loc_9:BitmapNumberTextRolling = null;
            var _loc_10:GBitmap = null;
            var _loc_11:int = 0;
            var _loc_2:* = parseInt(this._text);
            var _loc_3:* = param1;
            this._rollingToValue = param1;
            if (!this._loaded)
            {
                return;
            }
            if (_loc_2 == _loc_3)
            {
                return;
            }
            this.disposeRolling();
            this._rollingTweens = [];
            var _loc_4:Boolean = true;
            var _loc_5:int = 0;
            while ((_loc_2 > 0 || _loc_3 > 0) && _loc_2 != _loc_3)
            {
                
                if (_loc_3 == 0 && _loc_5 > 0)
                {
                    DisplayUtil.delNotUse(this._curBmps, _loc_5);
                    break;
                }
                _loc_9 = UICompomentPool.getUICompoment(BitmapNumberTextRolling);
                _loc_9.init(_loc_2 % 10, _loc_3 % 10, this._cellWidth + this._gap, this._cellHeight, _bmpds[this._urlName] as Array, _loc_4);
                _loc_2 = _loc_2 / 10;
                _loc_3 = _loc_3 / 10;
                _loc_4 = !_loc_4;
                this._rollingTweens.push(_loc_9);
                _loc_5++;
                if (_loc_5 > this._curBmps.length)
                {
                    _loc_10 = this.getBitmap(-1);
                }
            }
            this._text = param1.toString();
            this._needUpdate = true;
            this.updateText();
            this.updateLaout();
            var _loc_6:int = 0;
            var _loc_7:int = 1;
            if (this._align == Right)
            {
                _loc_7 = -1;
            }
            else if (this._align == Mid)
            {
                _loc_6 = (this._curBmps.length * (this._cellWidth + this._gap) - this._gap) / 2;
            }
            var _loc_8:int = 0;
            while (_loc_8 < this._rollingTweens.length)
            {
                
                _loc_11 = (this._curBmps.length - 1) - _loc_8;
                _loc_10 = this.getBitmap(_loc_11);
                _loc_9 = this._rollingTweens[_loc_8];
                _loc_9.x = _loc_7 * _loc_11 * (this._cellWidth + this._gap) - _loc_6;
                _loc_9.rolling(_loc_10);
                this.addChild(_loc_9);
                _loc_8++;
            }
            return;
        }// end function

        public function updateLaout() : void
        {
            var _loc_5:GBitmap = null;
            if (_isHideDispose)
            {
                return;
            }
            var _loc_1:int = 0;
            var _loc_2:int = 1;
            if (this._align == Right)
            {
                _loc_1 = this.width;
            }
            else if (this._align == Mid)
            {
                _loc_1 = this.width / 2;
            }
            var _loc_3:* = this._curBmps.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = this._curBmps[_loc_4];
                _loc_5.x = _loc_2 * _loc_4 * (this._cellWidth + this._gap) - _loc_1;
                _loc_4++;
            }
            return;
        }// end function

        private function updateText() : void
        {
            var _loc_2:String = null;
            var _loc_3:BitmapData = null;
            var _loc_4:GBitmap = null;
            if (_bmpds[this._urlName] == null || !this._needUpdate)
            {
                return;
            }
            this._needUpdate = false;
            if (this._text == null || this._text == "")
            {
                DisplayUtil.delNotUse(this._curBmps, 0, true);
                return;
            }
            var _loc_1:int = 0;
            while (_loc_1 < this._text.length)
            {
                
                _loc_2 = this._text.charAt(_loc_1);
                _loc_3 = this.getBitmapData(_loc_2);
                if (_loc_3 == null)
                {
                }
                else
                {
                    _loc_4 = this.getBitmap(_loc_1);
                    _loc_4.x = _loc_1 * (this._cellWidth + this._gap);
                    _loc_4.bitmapData = _loc_3;
                }
                _loc_1++;
            }
            DisplayUtil.delNotUse(this._curBmps, this._text.length, true);
            return;
        }// end function

        private function getBitmap(param1:int) : GBitmap
        {
            var _loc_2:GBitmap = null;
            if (param1 < 0)
            {
                _loc_2 = UIFactory.gBitmap("", param1 * (this._cellWidth + this._gap), 0, this);
                this._curBmps.unshift(_loc_2);
                this.updateLaout();
            }
            else
            {
                _loc_2 = this._curBmps[param1];
                if (_loc_2 == null)
                {
                    _loc_2 = UIFactory.gBitmap("", param1 * (this._cellWidth + this._gap), 0, this);
                    this._curBmps.push(_loc_2);
                }
            }
            return _loc_2;
        }// end function

        private function getBitmapData(param1:String) : BitmapData
        {
            var _loc_2:* = this._customCharIndex;
            if (_loc_2 == null)
            {
                _loc_2 = charIndex;
            }
            if (!_loc_2.hasOwnProperty(param1))
            {
                return null;
            }
            var _loc_3:* = int(_loc_2[param1]);
            return BitmapData(_bmpds[this._urlName][_loc_3]);
        }// end function

    }
}
